package github.flandre.examplemod.core.init.event;

import github.flandre.examplemod.common.mycapability.ISpeedUpCapability;
import github.flandre.examplemod.core.registry.CommonSetupEventHandler;
import github.flandre.examplemod.network.NetworkRegistryHandler;
import github.flandre.examplemod.network.Packet.Power;
import github.flandre.examplemod.network.Packet.PowerClinetForServer;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.FrameTimer;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.network.PacketDistributor;

import java.util.Timer;
import java.util.TimerTask;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class JoinWorldEvent {
    public static int count = 0;
    public static final int amount = 10;
    @SubscribeEvent
    public static void onJoinWorld(TickEvent.PlayerTickEvent event)
    {
        if(event.phase == TickEvent.Phase.END)
        {
            count++;
            if(count == 40)
            {
                count = 0;
                PlayerEntity playerEntity = event.player;
                LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
                capability.ifPresent((c)->{
                    float examplePower = c.getLevel();
                    if(examplePower > 1000)
                        c.setLevel(1000);
                    else
                        c.setLevel(examplePower + amount);
                    if(examplePower != 1000)
                    {
                        NetworkRegistryHandler.CHANNEL.sendToServer(new PowerClinetForServer(c.getLevel()));
                    }
                });
            }
        }

    }

}
